package com.bestcem.xm.ticket.controller.param.ticket.config;

import cn.hutool.core.collection.CollectionUtil;
import com.alibaba.fastjson.JSONObject;
import com.bestcem.xm.ticket.controller.param.ticket.config.createParam.TemplateDetailFieldParam;
import com.bestcem.xm.ticket.entity.mongo.Ticket;
import com.bestcem.xm.ticket.entity.mongo.TicketConfig;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.Data;
import lombok.EqualsAndHashCode;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author liheng
 * @version v1.0
 * @date 2021/8/24 20:20
 */
@EqualsAndHashCode(callSuper = true)
@Data
public class UpdateConfigParam extends CreateConfigParam {

    private Integer alertRuleCount;

    private List<String> alertRuleIds;

    private Integer createTrigger;

    @JsonProperty(value = "created_id")
    private String createdId;

    @JsonProperty(value = "updated_id")
    private String updatedId;

    @JsonProperty(value = "email_status")
    private Integer emailStatus;

    private String id;

    private String onlyHasContact;

    @JsonProperty(value = "record_flag")
    private Integer recordFlag;

    private String surveyCode;

    private String surveyCreator;

    @JsonProperty(value = "surveyHasticket")
    private Boolean surveyHasTicket;

    private Integer surveyStatus;

    private String surveyTitle;

    private String sysId;

    // 不要
    // @JsonProperty(value = "ticketFlow")
    // private List<UpdateTicketFlowParam> ticketFlows;

    private List<String> ticketFlowIds;

    // 不要
    // @JsonProperty(value = "ticketRule")
    // private List<UpdateTicketRuleParam> ticketRules;

    private String ticketRuleId;

    @JsonIgnore
    private List<JSONObject> dbStatusRuleList;

    @JsonProperty("created_dt")
    @JsonFormat(pattern = "yyyy/MM/dd HH:mm:ss", timezone = "GMT+8")
    private Date createdDate;

    @JsonProperty("updated_dt")
    @JsonFormat(pattern = "yyyy/MM/dd HH:mm:ss", timezone = "GMT+8")
    private Date updatedDate;

    /**
     * UpdateConfigParam -> TicketConfig
     *
     * @param param UpdateConfigParam
     * @return TicketConfig
     */
    public static TicketConfig paramToEntity(UpdateConfigParam param) {
        if (param == null) {
            return null;
        }

        TicketConfig ticketConfig = new TicketConfig();

        ticketConfig.setId(param.getId());
        ticketConfig.setRecordFlag(param.getRecordFlag());
        ticketConfig.setCreatedId(param.getCreatedId());
        ticketConfig.setUpdatedId(param.getUpdatedId());

        ticketConfig.setSysId(param.getSysId());
        ticketConfig.setOrgId(param.getOrgId());
        ticketConfig.setUserId(param.getUserId());
        ticketConfig.setSurveyId(param.getSurveyId());

        // 保存处理模板
        ticketConfig.setTicketTemplateId(param.getTicketTemplateId());

        List<String> list = param.getTicketFlowIds();
        if (list != null) {
            ticketConfig.setTicketFlowIds(new ArrayList<>(list));
        } else {
            ticketConfig.setTicketFlowIds(new ArrayList<>());
        }

        ticketConfig.setCreateTrigger(param.getCreateTrigger());
        ticketConfig.setAlertId(param.getAlertId());

        List<String> list1 = param.getAlertRuleIds();
        if (list1 != null) {
            ticketConfig.setAlertRuleIds(new ArrayList<>(list1));
        } else {
            ticketConfig.setAlertRuleIds(new ArrayList<>());
        }

        ticketConfig.setAlertRuleCount(param.getAlertRuleCount());

        if (param.getOnlyHasContact() != null) {
            ticketConfig.setOnlyHasContact(Boolean.parseBoolean(param.getOnlyHasContact()));
        }

        ticketConfig.setTitle(param.getTitle());
        ticketConfig.setState(param.getState());
        ticketConfig.setTicketRuleId(param.getTicketRuleId());
        ticketConfig.setNeedDeal(param.getNeedDeal());
        ticketConfig.setNeedRecord(param.getNeedRecord());
        ticketConfig.setRemindAdmin(param.getRemindAdmin());

        List<TemplateDetailFieldParam> list3 = param.getTemplateDetailFields();
        ArrayList<Ticket.TemplateDetailField> fields = new ArrayList<>();
        if (CollectionUtil.isNotEmpty(list3)) {
            for (TemplateDetailFieldParam templateDetailField : list3) {
                Ticket.TemplateDetailField field = new Ticket.TemplateDetailField();
                field.setContent(templateDetailField.getContent());
                field.setSourceId(templateDetailField.getSourceId());
                field.setOriginText(templateDetailField.getOriginText());
                fields.add(field);
            }
        }
        ticketConfig.setTemplateDetailFields(fields);

        ticketConfig.setSmtpId(param.getSmtpId());

        return ticketConfig;
    }

}
